// pages/pintuan/pintuan.js
const app = getApp()
var host = app.globalData.host;
var self = null;

Page({
  data: {
    themeColor: '',
    itemId: '',
    productId: '',
    groupId: '',
    group: null,
    product: null,
    user: [],
    countDownObj: ''
  },
  onLoad: function (options) {
    self = this;
    self.getStyle();
    if (options.itemId) {
      self.setData({
        itemId: options.itemId
      })
    }
    if (options.productId) {
      self.setData({
        productId: options.productId
      })
    }
    if (options.groupId) {
      self.setData({
        groupId: options.groupId
      })
    }
  },
  onShow: function () {
    self.init();
  },
  getStyle() {
    self.setData({
      themeColor: app.globalData.themeColor
    })
  },
  init() {
    var url = host + '/yq/group/joinList';
    var params = {
      productId: self.data.productId
    };
    if (self.data.groupId) {
      params.groupId = self.data.groupId;
    }
    var success = function (res) {
      console.log(res);
      self.setData({
        product: res.info.product
      })
      if (res.info.isGroupUp === 1) {
        var users = new Array;
        for (var i = 0; i < self.data.product.groupCount; i++) {
          users.push({});
        }
        for (var i = 0; i < res.info.user.length; i++) {
          users[i] = res.info.user[i];
        }
        self.setData({
          group: res.info.group,
          user: users
        })
        self.countDown();

      } else {
        wx.showToast({
          title: res.info.msg,
          icon: 'none'
        })
      }
    };
    var fail = function (res) {
      console.log(res);
    };
    var complete = function () { };
    app.request(url, params, success, fail, complete);
  },
  goPay(e) {
    if (self.data.product.storage == 0) {
      wx.showModal({
        title: '提示',
        content: '补货中',
        showCancel: false
      })
      return;
    }
    // 如果需要填写地址和数量
    // if (self.data.product.isNeedMoreInfo === 1) {
    //   wx.navigateTo({
    //     url: '/pages/confirm/confirm?itemId=' + self.data.itemId + '&productId=' + self.data.productId + '&groupId=' + self.data.groupId
    //   })
    //   return;
    // }
    if (self.data.product.isNeedMoreInfo != 0) {
      wx.navigateTo({
        url: '/pages/extraInfo/extraInfo?itemId=' + self.data.itemId + '&productId=' + self.data.productId + '&groupId=' + self.data.groupId
      })
      return;
    }

    wx.showLoading({
      title: '提交中',
    })
    var url = host + '/yq/order/pay';
    var data = {
      // name: self.data.name,
      // mobile: self.data.mobile,
      // code: self.data.code,
      // price1Count: self.data.price1Count,
      // price2Count: self.data.price2Count,
      // price3Count: self.data.price3Count,
      // ticketId: self.data.ticketId
    };
    if (self.data.productId) {
      data.productId = self.data.productId
    }
    if (self.data.itemId) {
      data.itemId = self.data.itemId
    }
    if (self.data.groupId) {
      data.groupId = self.data.groupId;
    }
    var success = function (res) {
      console.log(res);
      wx.hideLoading()
      var status = res.status
      if (status == 204) {
        wx.showModal({
          title: '出错了',
          content: res.msg
        })
        return;
      }
      var response = res.info
      var nonce_str = response.nonce_str
      var prepay_id = response.prepay_id
      var sign = response.sign
      var timestamp = response.timeStamp
      var appId = response.appid
      var param = {
        'appId': appId,
        'timeStamp': '' + timestamp,
        'nonceStr': '' + nonce_str,
        'package': 'prepay_id=' + prepay_id,
        'signType': 'MD5',
        'paySign': '' + sign,
        'success': function (res) {
          setTimeout(function () {
            wx.navigateTo({
              url: '/pages/success/success?isShowContent=' + 0
            })
          }, 200)

        },
        'fail': function (res) {
          wx.showModal({
            title: '出错了',
            content: '支付失败'
          })
        }
      }
      wx.requestPayment(param);
    };
    var fail = function (res) { };
    var complete = function () { };
    app.request(url, data, success, fail, complete);
  },
  timeFormat(param) {
    return param < 10 ? '0' + param : param;
  },
  countDown() {//倒计时函数
    if (!self.data.group) {
      return;
    }
    // 获取当前时间，同时得到活动结束时间数组
    let newTime = new Date().getTime();
    let endTimeStr = self.data.group.expireTimestamp;
    // let endTime = self.data.group.expire;
    let countDownObj = null;

    // 对结束时间进行处理渲染到页面
    let endTime = new Date(endTimeStr).getTime();
    // 如果活动未结束，对时间进行处理
    if (endTime - newTime > 0) {
      let time = (endTime - newTime) / 1000;
      // 获取天、时、分、秒
      let day = parseInt(time / (60 * 60 * 24));
      let hou = parseInt(time % (60 * 60 * 24) / 3600);
      let min = parseInt(time % (60 * 60 * 24) % 3600 / 60);
      let sec = parseInt(time % (60 * 60 * 24) % 3600 % 60);
      countDownObj = {
        day: this.timeFormat(day),
        hou: this.timeFormat(hou),
        min: this.timeFormat(min),
        sec: this.timeFormat(sec)
      }
    } else {//活动已结束，全部设置为'00'
      countDownObj = {
        day: '00',
        hou: '00',
        min: '00',
        sec: '00'
      }
    }

    // 渲染，然后每隔一秒执行一次倒计时函数
    this.setData({
      countDownObj: countDownObj
    });
    setTimeout(this.countDown, 1000);
  },
  onShareAppMessage(obj) {
    var url = '/pages/productInfo/productInfo?productId=' + self.data.productId;
    if (self.data.group) {
      url += '&groupId=' + self.data.group.id;
    }
    console.log(url);
    return {
      title: self.data.product.shareTitle,
      imageUrl: self.data.product.shareImage,
      path: url
    }
  }
})
